package study.javaee.practice.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import study.javaee.practice.entity.User;
import study.javaee.practice.property.CacheProperty;
import study.javaee.practice.service.RedisService;
import study.javaee.practice.service.UserCacheService;

@Service
public class UserCacheServiceImpl implements UserCacheService {

    final String EMAIL_CODE_CACHE;
    final String IMG_CODE_CACHE;
    final String USER_CACHE;
    final CacheProperty cacheProperty;
    final RedisService redisService;

    @Autowired
    public UserCacheServiceImpl(CacheProperty cacheProperty, RedisService redisService) {
        this.cacheProperty = cacheProperty;
        this.redisService = redisService;
        EMAIL_CODE_CACHE = cacheProperty.getDbName() + cacheProperty.getEmailCode();
        IMG_CODE_CACHE = cacheProperty.getDbName() + cacheProperty.getImgCode();
        USER_CACHE = cacheProperty.getDbName() + cacheProperty.getUser();
    }

    @Override
    public boolean setImgAuthCode(String email, String code) {
        redisService.set(IMG_CODE_CACHE + email, code);
        return true;
    }

    @Override
    public String getImgAuthCode(String email) {
        return (String) redisService.get(IMG_CODE_CACHE + email);
    }

    @Override
    public void setEmailVerCode(String email, String usage, String code) {
        redisService.set(EMAIL_CODE_CACHE+ email + usage, code, cacheProperty.getEmailExpire());
    }

    @Override
    public String getEmailVerCode(String email, String usage) {
        return (String) redisService.get(EMAIL_CODE_CACHE+ email + usage);
    }

    @Override
    public void deleteEmailVerCode(String email, String usage) {
        redisService.delete(EMAIL_CODE_CACHE+ email + usage);
    }

    @Override
    public void setUser(String email, User user) {
        redisService.set(USER_CACHE + email, user);
    }

    @Override
    public User getUser(String email) {
        return (User) redisService.get(email);
    }

    @Override
    public void deleteUser(String email) {
        redisService.delete(email);
    }
}
